/*
 * $Id: ReverseAxis.java,v 1.2 2003/05/31 15:22:07 znamenacek Exp $
 *
 * Copyright (c) Tomas Znamenacek
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package net.sf.vofce.canvas;

import org.apache.log4j.Category;

/**
 * An axis that grows from right to left.
 * The <code>Axis</code> class represents an axis that grows from left to right,
 * or from the top to the bottom (classic <code>x</code> axis being an example).
 * This class represents an axis that grows in the opposite direction (classic
 * <code>y</code> axis being the example).
 * 
 * @author Tomas Znamenacek
 */
public class ReverseAxis extends Axis {

	/**
	 * Creates new axis with specified physical size, leaving logical
	 * size at default values.
	 */
	public ReverseAxis(int physicalLength) {

		super(physicalLength);
	}
	
	/**
	 * Creates new axis with specified physical and logical size.
	 */
	public ReverseAxis(int physicalLength, int position, double scale) {

		super(physicalLength, position, scale);
	}

	/**
	 * Converts between physical and logical coordinates.
	 */
	public int logicalToPhysical(double log) {

		double highest = (position + Math.floor(markNumber() / 2)) * scale;
		int result = (int) Math.round((highest - log) / sampleSize);
		return ((result >= 0) && (result < physicalLength)) ? result : -1;
	}
	
	/**
	 * Recalcualtes the axis layout, the heart of this class.
	 */
	protected void recalculate() {

		int m = 0;
		
		int firstMark = (int) (position + Math.floor(markNumber() / 2)); 
		int lastMark = firstMark - markNumber();
		
		double mark = firstMark * scale;

		sampleSize = scale / markDistance;
		marks = new int[markNumber()+1];

		zeroPosition = (firstMark * lastMark <= 0) ? firstMark * markDistance : -1;
		
		if (values.length != physicalLength) {
			values = new double[physicalLength];
		}

		for (int i = 0; i < values.length; i++) {

			if (i % markDistance == 0) {
				
				marks[m++] = i;
				values[i] = mark;
				mark -= scale;
				
			} else {

				values[i] = values[i-1] - sampleSize;
			}
		}
	}
}
